﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using Database.Classes;


public partial class StoredProcedures
{
    public static bool CheckIfSystemExists(int x, int y)
    {
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("Select count(*) From Systems Where [X] = @x and [Y] = @Y", conn))
            {
                cmd.Parameters.Add("@x", SqlDbType.Int).Value = x;
                cmd.Parameters.Add("@y", SqlDbType.Int).Value = y;
                int t = int.Parse(cmd.ExecuteScalar().ToString());

                conn.Close();
                if (t == 0)
                    return false;
                else
                    return true;
            }
        }
    }
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void CreateSystem(int x, int y, string name)
    {
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("Insert into Systems Values(@name, @x, @y)", conn))
            {
                cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = name;
                cmd.Parameters.Add("@x", SqlDbType.Int).Value = x;
                cmd.Parameters.Add("@y", SqlDbType.Int).Value = y;
                cmd.ExecuteNonQuery();

                conn.Close();
            }
        }
        InsertLog(DateTime.Now, "CreateSystem", x + " " + y + " " + name);
    }

    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void CreatePlanets(int sId, int number)
    {
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            for (int i = 1; i <= number; i++)
            {
                using (SqlCommand cmd = new SqlCommand("Insert into Planets (PlanetId,SystemID) Values(@x, @y)", conn))
                {
                    cmd.Parameters.Add("@x", SqlDbType.Int).Value = i;
                    cmd.Parameters.Add("@y", SqlDbType.Int).Value = sId;
                    cmd.ExecuteNonQuery();
                }
            }
            conn.Close();
        }
    }
};
